//
//  RecommndCityListCell.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/1.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "RecommndCityListCell.h"

@implementation RecommndCityListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (CGFloat)getHight:(NSArray *)cityArray{
   
    if (cityArray.count == 0) {
        return 0.01;
    }
    
    
    int x = 15;
    int H = 15;
    for (int i = 0; i < cityArray.count; i++) {
        
        City * city = cityArray[i];
        
        CGFloat width = [IGUtils getWidthWithString:city.smartName font:Font(12)];//[YYCTools getWidthWithString:titleArr[i] font:NORMAL_FONT_SIZE(14)] + 11;
        
        CGFloat L = x + width + 15;
        
        IGLog(@"-------%f---%d---%d---%F",width,x,H,L);
        
        
        
        if (L >= SCREEN_WIDTH) {
            x = 15;
            H = H + 43 ;
        }
        
        x = x + width + 10;
        
    }
    
    
    return H + 45;
}
- (void)configData:(NSArray *)cityArray  {
    for (UIView *view in self.contentView.subviews) {
        
        [view removeFromSuperview];
    }
    
    
    self.cityArray = cityArray;
    
    int x = 15;
    int y = 15;
    for (int i = 0; i < cityArray.count; i++) {
        
        City * city = cityArray[i];
        
        UIButton * btn = [self createSelectBtnWithTitle:city.smartName];
        [self.contentView addSubview:btn];
        if (x + CGRectGetWidth(btn.bounds) + 15 >= SCREEN_WIDTH) {
            x = 15;
            y += (CGRectGetHeight(btn.bounds) + 13);
        }
        
        btn.tag = i;
        btn.frame = CGRectMake(x, y, CGRectGetWidth(btn.bounds), CGRectGetHeight(btn.bounds));
        x = x + CGRectGetWidth(btn.bounds) + 10;
        
    }
    
}


-(UIButton *)createSelectBtnWithTitle:(NSString *)title {
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat width = [IGUtils getWidthWithString:title font:Font(12)];
    btn.bounds = CGRectMake(0, 0, width , 30);
    btn.clipsToBounds = YES;
    
    btn.layer.cornerRadius = 5;
    btn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    btn.layer.borderWidth = 1.f;
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitleColor:kSubitleColor forState:UIControlStateNormal];
    
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = Font(12);
    
    [btn addTarget:self action:@selector(titleBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return btn;
}

- (void)titleBtnClicked:(UIButton *)sender {
    
    
    City * city = self.cityArray[sender.tag];
    
    if (_delegate && [_delegate respondsToSelector:@selector(cityListSelectCity:)]) {
        
        [_delegate cityListSelectCity:city];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
